<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redirect;

class LoginController extends Controller
{
    //登录页面
    public function index()
    {
        return view('login.index');
    }
    //登录行为
    public function login()
    {
        //验证
        $this->validate(request(),[
            'username'=>'required|string|min:5|max:12',
            'password'=>'required|min:6|string',
            'is_remember'=>'integer'
        ]);
        //逻辑
        $user=request(['username','password']);
        $isremember=request('is_remember');
//        $username=request('username');TODO
//        $password=request('password');
        if(Auth::attempt($user,$isremember)){
            return redirect('/posts');
        }
        return Redirect::back()->withErrors("用户名密码不匹配");
    }
    //登出
    public function logout()
    {
        Auth::logout();
        return redirect('/login');
    }
}
